package com.dm.cloud.auth.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.dm.cloud.auth.common.mode.AppUserStatistics;
import com.dm.cloud.auth.common.mode.AuthAppClientDetail;
import com.dm.cloud.auth.common.po.AuthClientDetails;
import com.dm.cloud.auth.common.po.AuthUser;
import com.dm.cloud.auth.service.AuthClientDetailsService;
import com.dm.cloud.auth.service.AuthUserService;
import com.dm.cloud.tkmybatis.service.BaseMapper;
import com.dm.cloud.auth.mapper.AuthAppMapper;
import com.dm.cloud.auth.service.AuthAppService;
import com.dm.cloud.auth.common.po.AuthApp;
import com.dm.cloud.core.exception.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
   * @author hcx
   * @type AuthAppServiceImpl.java
   * @desc AuthApp serviceImpl
   */
@Service

@Transactional

public class AuthAppServiceImpl implements AuthAppService {


    @Autowired
    private AuthAppMapper authAppMapper;

    @Autowired
    private AuthClientDetailsService authClientDetailsService;

    @Autowired
    private AuthUserService authUserService;

    @Override
    public BaseMapper getMapper() {
        return authAppMapper;
    }
    public void processBeforeOperation(AuthApp entity, BaseOperationEnum operationEnum) throws CustomException {
        if(operationEnum.equals(BaseOperationEnum.INSERT)){
            if(StrUtil.isEmpty(entity.getName())){
                throw new CustomException("应用名称不能为空！");
            }
            String clientId = StrUtil.isEmpty(entity.getClientId()) ? IdUtil.fastSimpleUUID() : entity.getClientId();
            AuthClientDetails detail = authClientDetailsService.insert(AuthClientDetails.builder()
                    .allowUrl("*")
                    .clientId(clientId)
                    .authorizedGrantTypes("authorization_code,password,client_credentials,implicit,refresh_token")
                    .clientSecret(IdUtil.fastUUID())
                    .scope("SCOPE_ALL")
                    .build());
            entity.setClientId(clientId);
        }

    }

    public void processAfterOperation(AuthApp entity, BaseOperationEnum operationEnum) throws CustomException {
    }

    @Override
    public AuthAppClientDetail appClientDetail(String appId) {
        AuthApp app = authAppMapper.selectByPrimaryKey(appId);
        AuthClientDetails clientDetail = authClientDetailsService.getById(app.getClientId());
        return AuthAppClientDetail.builder()
                .clientId(clientDetail.getClientId())
                .clientSecret(clientDetail.getClientSecret())
                .build();
    }

    @Override
    public AuthApp findByAppCode(String appCode) {
        AuthApp app = authAppMapper.findByAppCode(appCode);
        return app;
    }

    @Override
    public Map<String, String> appCodeName() {
        List<AuthApp> apps=authAppMapper.findAllCodeAndName();
        Map<String,String> reMap=new HashMap<>();
        for (AuthApp app : apps) {
            reMap.put(app.getAppCode(),app.getName());
        }
        return reMap;
    }

    /**
     * 统计应用关联的用户信息
     * @param appCode
     * @return
     */
    @Override
    public AppUserStatistics appUserStatistics(String appCode) {
        AppUserStatistics statistics=new AppUserStatistics();
        List<AuthUser> users=authUserService.findByAppCode(appCode);
        if(users!=null && !users.isEmpty()){
            for (AuthUser user : users) {
                if(1==user.getFreeze()){
                    statistics.addFreezee();
                }else{
                    statistics.addActivate();
                }
            }
        }
        return statistics;
    }
}
